package com.springtest.demo.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonView;
import com.springtest.demo.model.Course;
import com.springtest.demo.model.ResultEntity;
import com.springtest.demo.serices.CourseService;
import com.springtest.demo.views.CourseView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/cource")
@PropertySource("/WEB-INF/databases.properties")
public class CourseController {
	
	@Value("${jdbc.url}")
	private String url;
	
	@Value("${jdbc.username}")
	private String username;
	
	@Bean
	public static PropertySourcesPlaceholderConfigurer gerProperty(){
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Resource(name="courceService")
	private CourseService courceService;
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody ResultEntity save(@RequestBody Course c){
		
		return courceService.save(c);
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody ResultEntity update(@RequestBody Course c){
		
		return courceService.update(c);
	}
	
	@RequestMapping(value="delete/{id}",method=RequestMethod.GET)
	public @ResponseBody ResultEntity delete(@PathVariable int id){
		
		return courceService.delete(id);
	}
	
	@RequestMapping(value="delete",method=RequestMethod.GET)
	public @ResponseBody ResultEntity delete(Course c){
		
		return courceService.delete(c);
	}
	
	@JsonView(CourseView.CourseName.class)
	@RequestMapping(value="findById/{id}",method=RequestMethod.GET)
	public @ResponseBody ResultEntity findById(@PathVariable int id){
		
		return courceService.findById(id);
	}
	
	@RequestMapping(value="findAll",method=RequestMethod.GET)
	public @ResponseBody ResultEntity findAll(){
		
		return courceService.findAll();
	}
	
	@RequestMapping(value="page",method=RequestMethod.GET)
	public @ResponseBody ResultEntity page(int page,int size,@ModelAttribute("objs")ArrayList<Object> objs){
		gerProperty();
		return courceService.page(page,size,objs);
	}
	
}
